package com.fr71.project.dm.listener;

import com.fr71.common.utils.DateUtils;
import com.fr71.project.dm.domain.PdUser;
import com.fr71.project.dm.service.IPdUserService;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.util.List;

@ChannelHandler.Sharable
public class AuthTcpRequest extends ChannelInboundHandlerAdapter {

    private static final Logger logger = LoggerFactory.getLogger(AuthTcpRequest.class);

    private final IPdUserService iPdUserService;

    public AuthTcpRequest(IPdUserService iPdUserService) {
        this.iPdUserService = iPdUserService;
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        logger.debug("验证链接关闭");
        AuthUtil.removeChannel(ctx);
        super.channelInactive(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        logger.debug("验证链接进入");
        AuthUtil.addChannel(ctx);
        super.channelActive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof ByteBuf) {
            ByteBuf byteBuf = (ByteBuf) msg;
            try {
                byte[] data = ByteBufUtil.getBytes(byteBuf);
                updateUserData(ctx, data);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                byteBuf.release();
            }
        }
    }

    public void updateUserData(ChannelHandlerContext ctx, byte[] data) {
        String ip = getRemoteAddr(ctx);
        TcpDataReader reader = new TcpDataReader(data);
        reader.stepSize(10);

        long head = reader.readNumber(2);

        if (head == 1L) {
            long totalConnections = reader.readNumber(4);
            long connections = reader.readNumber(4);
            long totalPlayers = reader.readNumber(4);
            String memInfo = reader.readString(2);
            String jvmInfo = reader.readString(2);
            String cpuInfo = reader.readString(2);
            String version = reader.readString(2);

            PdUser queryUser = new PdUser();
            queryUser.setBindIp(ip);
            queryUser.setUType(1);
            List<PdUser> userList = iPdUserService.selectPdUserList(queryUser);
            
            for (PdUser it : userList) {
                it.setTotalConnections(totalConnections);
                it.setConnections(connections);
                it.setTotalPlayers(totalPlayers);
                it.setMemInfo(memInfo);
                it.setJvmInfo(jvmInfo);
                it.setCpuInfo(cpuInfo);
                it.setVersion(version);
                iPdUserService.updatePdUser(it);
            }
        }
    }

    private String getRemoteAddr(ChannelHandlerContext ctx) {
        return ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress();
    }
}